package fiszki.namespace;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
	private static final String DEBUG_TAG = "SqLiteSetsManager";
    private static final int DB_VERSION = 1;
    private static final String DB_NAME = "database.db";
    private static final String DB_SETS_TABLE = "sets";
    
    public static final String KEY_ID = "_id";
    public static final String ID_OPTIONS = "INTEGER PRIMARY KEY AUTOINCREMENT";
    public static final String KEY_NAME = "name";
    public static final String NAME_OPTIONS = "TEXT NOT NULL";
    public static final String KEY_QUESTIONLANG = "questionlang";
    public static final String QUESTIONLANG_OPTIONS = "INTEGER DEFAULT 0";
    public static final String KEY_ANSWERLANG = "answerlang";
    public static final String ANSWERLANG_OPTIONS = "INTEGER DEFAULT 0";
	
    private static final String DB_FISZKI_TABLE = "fiszki";
    
    public static final String KEY_F_ID = "_id";
    public static final String F_ID_OPTIONS = "INTEGER PRIMARY KEY AUTOINCREMENT";
    public static final String KEY_SET_ID = "set_id";
    public static final String SET_OPTIONS = "TEXT NOT NULL";
    public static final String KEY_QUESTION = "question";
    public static final String QUESTION_OPTIONS = "TEXT NOT NULL";
    public static final String KEY_ANSWER = "answer";
    public static final String ANSWER_OPTIONS = "TEXT NOT NULL";
    
    private static final String DB_CREATE_SETS_TABLE =
            "CREATE TABLE " + DB_SETS_TABLE + "( " +
            KEY_ID + " " + ID_OPTIONS + ", " +
            KEY_NAME + " " + NAME_OPTIONS + ", " +
            KEY_QUESTIONLANG + " " + QUESTIONLANG_OPTIONS + ", " +
            KEY_ANSWERLANG + " " + ANSWERLANG_OPTIONS +
            ");";
    private static final String DROP_SETS_TABLE =
            "DROP TABLE IF EXISTS " + DB_SETS_TABLE;
    
    private static final String DB_CREATE_FISZKI_TABLE =
            "CREATE TABLE " + DB_FISZKI_TABLE + "( " +
            KEY_F_ID + " " + F_ID_OPTIONS + ", " +
            KEY_SET_ID + " " + SET_OPTIONS + ", " +
            KEY_QUESTION + " " + QUESTION_OPTIONS + ", " +
            KEY_ANSWER + " " + ANSWER_OPTIONS +
            ");";
    private static final String DROP_FISZKI_TABLE =
            "DROP TABLE IF EXISTS " + DB_FISZKI_TABLE;
	
    public DatabaseHelper(Context context, CursorFactory factory) {
        super(context, DB_NAME, factory, DB_VERSION);
    }
 
    @Override
    public void onCreate(SQLiteDatabase db) {
    	db.execSQL(DB_CREATE_SETS_TABLE);
        db.execSQL(DB_CREATE_FISZKI_TABLE);
 
        Log.d(DEBUG_TAG, "Database creating...");
        Log.d(DEBUG_TAG, "Table " + DB_SETS_TABLE + " ver." + DB_VERSION + " created");
        Log.d(DEBUG_TAG, "Table " + DB_FISZKI_TABLE + " ver." + DB_VERSION + " created");
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(DROP_SETS_TABLE);
        db.execSQL(DROP_FISZKI_TABLE);
 
        Log.d(DEBUG_TAG, "Database updating...");
        Log.d(DEBUG_TAG, "Table updated from ver." + oldVersion + " to ver." + newVersion);
        Log.d(DEBUG_TAG, "All data is lost.");
 
        onCreate(db);
    }
}